package it.ortofruit.servers;

 import it.ortofruit.domain.SimpleItem;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Logger;

import com.google.gson.Gson;

public class ServerJson 
{
	public static final int UPD_PORT = 4445;
	static Logger logger = Logger.getLogger("global");
	
	public static void main(String[] args) throws IOException, ClassNotFoundException
	{
		ServerSocket srvSck = new ServerSocket(UPD_PORT);
		logger.info("socket istanziato, accetto connessioni...");
		while(true)
		{
			Socket socket = srvSck.accept();
			logger.info("accettata nuova connessione...");
			ObjectOutputStream outStream = new ObjectOutputStream (socket.getOutputStream());
			ObjectInputStream inStream = new ObjectInputStream (socket.getInputStream());
			String ricevuto = (String)inStream.readObject();
			logger.info("ricevuto: "+ricevuto);
			Gson gson = new Gson();
			SimpleItem risultato = gson.fromJson(ricevuto, SimpleItem.class);
			logger.info("risultato:"+ risultato);
			outStream.writeObject("OK");
			outStream.flush();
			socket.close();
		}
		
	}
	   
}
